package com.codelab.statemachine.web;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.NumberUtil;
import com.codelab.statemachine.config.ComplexFormEvents;
import com.codelab.statemachine.config.ComplexFormStateMachineBuilder;
import com.codelab.statemachine.config.ComplexFormStates;
import com.codelab.statemachine.config.Form;
import com.codelab.statemachine.mybatis.dataobject.TaskDO;
import com.codelab.statemachine.task.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/statemachine")
public class StateMachineController {

/*    @Autowired
    private BeanFactory beanFactory;

    @Autowired
    private ComplexFormStateMachineBuilder complexFormStateMachineBuilder;*/

    @Autowired
    private CommonTaskService commonTaskService;

    /*@RequestMapping("/testComplexFormState")
    public void testComplexFormState() throws Exception {

        StateMachine<ComplexFormStates, ComplexFormEvents> stateMachine = complexFormStateMachineBuilder.build(beanFactory);
        System.out.println(stateMachine.getId());

        Form form1 = new Form();
        form1.setId("111");
        form1.setFormName(null);

        Form form2 = new Form();
        form2.setId("222");
        form2.setFormName("好的表单");

        Form form3 = new Form();
        form3.setId("333");
        form3.setFormName(null);

        // 创建流程
        System.out.println("-------------------form1------------------");

        stateMachine.start();
        Message message = MessageBuilder.withPayload(ComplexFormEvents.WRITE).setHeader("form", form1).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.CHECK).setHeader("form", form1).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.DEAL).setHeader("form", form1).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.SUBMIT).setHeader("form", form1).build();
        stateMachine.sendEvent(message);
        System.out.println("最终状态：" + stateMachine.getState().getId());

        System.out.println("-------------------form2------------------");
        stateMachine = complexFormStateMachineBuilder.build(beanFactory);
        stateMachine.start();
        message = MessageBuilder.withPayload(ComplexFormEvents.WRITE).setHeader("form", form2).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.CHECK).setHeader("form", form2).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.DEAL).setHeader("form", form2).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.SUBMIT).setHeader("form", form2).build();
        stateMachine.sendEvent(message);
        System.out.println("最终状态：" + stateMachine.getState().getId());

        System.out.println("-------------------form3------------------");
        stateMachine = complexFormStateMachineBuilder.build(beanFactory);
        stateMachine.start();
        message = MessageBuilder.withPayload(ComplexFormEvents.WRITE).setHeader("form", form3).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.CHECK).setHeader("form", form3).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        form3.setFormName("好的表单");
        message = MessageBuilder.withPayload(ComplexFormEvents.DEAL).setHeader("form", form3).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.SUBMIT).setHeader("form", form3).build();
        stateMachine.sendEvent(message);
        message = MessageBuilder.withPayload(ComplexFormEvents.CHECK).setHeader("form", form3).build();
        stateMachine.sendEvent(message);
        System.out.println("当前状态：" + stateMachine.getState().getId());
        message = MessageBuilder.withPayload(ComplexFormEvents.SUBMIT).setHeader("form", form3).build();
        stateMachine.sendEvent(message);
        System.out.println("最终状态：" + stateMachine.getState().getId());
    }
*/
    @RequestMapping("/test")
    public String testCommonTaskService() throws Exception {
        CommonTask commonTask = new CommonTask();
        commonTask.setStatus(TaskStates.CREATE.toString());
        commonTask.setParams(MapBuilder.create(new HashMap<>()).put("cc","11").build());
        commonTask.setMessage("父任务");
        commonTask.setTaskCode("parent");

        CommonTask commonTask1 = new CommonTask();
        commonTask1.setStatus(TaskStates.CREATE.toString());
        commonTask1.setParams(MapBuilder.create(new HashMap<>()).put("dd","11").build());
        commonTask1.setMessage("子任务1");
        commonTask1.setTaskCode("demo1");

        CommonTask commonTask2 = new CommonTask();
        commonTask2.setStatus(TaskStates.CREATE.toString());
        commonTask2.setParams(MapBuilder.create(new HashMap<>()).put("ee","11").build());
        commonTask2.setMessage("子任务2");
        commonTask2.setTaskCode("demo2");

        CommonTask commonTask3 = new CommonTask();
        commonTask3.setStatus(TaskStates.CREATE.toString());
        commonTask3.setParams(MapBuilder.create(new HashMap<>()).put("ff","11").build());
        commonTask3.setMessage("子任务3");
        commonTask3.setTaskCode("demo3");

        commonTask.getChildsTask().add(commonTask1);
        commonTask.getChildsTask().add(commonTask2);
        commonTask.getChildsTask().add(commonTask3);

        String taskid  = commonTaskService.createTaskMachine(commonTask);
        System.out.println("执行testCommonTaskService");
        return "主任务id"+taskid;
    }

    @RequestMapping("/testUser")
    public List<TaskDO> testUserTaskService() throws Exception {
        return commonTaskService.listAllTask();
    }
}